﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : Lolo
// Created          : 09-21-2013
//
// Last Modified By : Lolo
// Last Modified On : 10-24-2013
// ***********************************************************************
// <copyright file="MyProfileController.cs" company="Innovacall">
//     Copyright (c) Innovacall. All rights reserved.
// </copyright>
// <summary>I created her as an axample of use of webapi</summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Web.Http;
using System.Web.Security;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.DomainLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Portals.MVC4Portal.Models.JSonModels;
using InnovaApp.Portals.MVC4Portal.Models.WebAPIModels.JSTO;
using INNOVACALL.Framework.Common;

namespace InnovaApp.Portals.MVC4Portal.Controllers.WebAPIControllers.ContactManagement
{
    /// <summary>
    /// Class MyProfileController.
    /// </summary>
    public class MyProfileController : _WebAPIControllers_Base<Contact_WebAPIModel>
    {
        protected static string eMethodBase = "InnovaApp.Portals.MVC4Portal.Controllers.WebAPIControllers.ContactManagement.MyProfileController";
        /// <summary>
        /// Posts the specified language.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="model">The model.</param>
        /// <returns>HttpResponseMessage.</returns>
        /// <exception cref="INNOVACALL.Framework.Common.InnovacallExceptions.IdentificationException"></exception>
        [HttpPost]
        public HttpResponseMessage Post(string language, MyProfileSave_JSonModel model)
        {
            HttpResponseMessage toReturn;
            string eMethod = eMethodBase + "Post";
            object eParameters = new { model = model };
            try
            {
                MembershipUser currentUser = Membership.GetUser();
                using (DataRepositories _dataContext = new DataRepositories())
                {
                    Contact dbContact = _dataContext.Contact_Rep.Query(m => m.InnoAppUser.Id == (Guid)(currentUser.ProviderUserKey)).FirstOrDefault();
                    if (dbContact == null)
                    {
                        throw new INNOVACALL.Framework.Common.InnovacallExceptions.IdentificationException(eMethod, eParameters, Resources.View_All_UserNotFound);
                    }
                    dbContact.FK_Genre = model.FK_Genre;
                    dbContact.FirstName = model.FirstName;
                    dbContact.LastName = model.LastName;
                    DateTime birthDate;
                    if (DateTime.TryParse(model.BirthDate, out birthDate))
                    {
                        dbContact.BirthDate = birthDate;
                    }
                    dbContact.IconPath = model.IconPath;
                    double lat, lng;
                    if (double.TryParse(model.GoogleMapLat, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out lat) && double.TryParse(model.GoogleMapLng, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out lng))
                    {
                        dbContact.GoogleMapLat = lat;
                        dbContact.GoogleMapLng = lng;
                    }
                    dbContact.FK_Language = model.FK_Language;
                    _dataContext.Save();
                }
                base.ApiData.success = true;
                base.ApiData.message = Resources.View_MyProfile_ImagesSaved;
                toReturn = base.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                base.ApiData.success = false;
                base.ApiData.message = ex.Message;
                base.ApiData.iFrameContent = ex.StackTrace;
                base.ApiData.scriptToExecute = StringEnum.GetStringValue(enScriptToExecute.DisplayMessage);
                toReturn = base.CreateResponse(HttpStatusCode.OK);
            }
            return toReturn;
        }
    }
}
